package com.engine.budget.cmd;

import com.engine.common.biz.AbstractCommonCommand;
import com.engine.common.entity.BizLogContext;
import com.engine.core.interceptor.CommandContext;
import weaver.conn.RecordSet;
import weaver.general.Util;
import weaver.hrm.User;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;


public class GetYszybCmd extends AbstractCommonCommand<Map<String,Object>> {

    Map<String, Object> req = new HashMap<>();

    public GetYszybCmd(User user, Map<String,Object> params) {
        this.user = user;
        this.params = params;
    }

    @Override
    public BizLogContext getLogContext() {
        return null;
    }


    @Override
    public Map<String, Object> execute(CommandContext commandContext) {

        RecordSet rs = new RecordSet();
        //4.定义SQL语句

        String sql="select * from uf_yszyb where 1 = 1 ";
        String sqlTotal="select count(1) total from uf_yszyb where 1 = 1 ";


        ArrayList<String> sqlArrs = new ArrayList<>();
        ArrayList<String> sqlTotalArrs = new ArrayList<>();
        String budgetPeriod = (String)params.get("budgetPeriod");
        if(budgetPeriod != null&&budgetPeriod != ""){
            sql+=(" AND ysnd = ? ");
            sqlTotal+=(" AND ysnd = ? ");
            sqlArrs.add(budgetPeriod);
            sqlTotalArrs.add(budgetPeriod);
        }

        String org = (String)params.get("org");
        if(org!=null&&!org.isEmpty()){
            sql+=(" AND zz = ? ");
            sqlTotal+=(" AND zz = ? ");
            sqlArrs.add(org);
            sqlTotalArrs.add(org);
        }



        RecordSet recordSet = new RecordSet();
        recordSet.executeQuery(sqlTotal,sqlTotalArrs);
        while (recordSet.next()) {
            req.put("total",recordSet.getString("total"));
        }


        int pageIndex = Util.getIntValue(params.get("pageIndex").toString(), 0);
        int pageSize = Util.getIntValue(params.get("pageSize").toString(), 0);
        if(pageIndex!=0&&pageSize!=0){
            sql+="LIMIT "+pageSize+" OFFSET "+((pageIndex-1)*pageSize);
        }else {
            sql+=("LIMIT 10 OFFSET 0 ");
        }

        rs.executeQuery(sql,sqlArrs);
        //rs.executeUpdate();
        ArrayList<Map> list = new ArrayList<>();
        //7.处理结果，并获取数据
        while (rs.next()){
            ArrayList<String> sqlSons = new ArrayList<>();
            Map<String, Object> map = new HashMap<>();
            Map<String, Object> mains = new HashMap<>();
            String id = rs.getString("id");
            mains.put("zz",Util.null2String(rs.getString("zz")));
            mains.put("dyysbzr",Util.null2String(rs.getString("dyysbzr")));
            mains.put("djbh",Util.null2String(rs.getString("djbh")));
            mains.put("zt",Util.null2String(rs.getString("zt")));
            mains.put("ysnd",Util.null2String(rs.getString("ysnd")));
            map.put("main",mains);
            //6.执行SQL语句命令
            //查询从表并封装
            RecordSet recordSet1 = new RecordSet();

            String sqlDt1 = "select s.ysnd,s.dwjzdgz,s.ysxm,k.yskmbm,s.yssx,s.zxdw,s.zxdw,s.dh,s.rq,s.zyqcje,s.zydzje,s.zyje,s.zhje,s.zyye,s.zhdjlx from uf_yszyb_dt1 s LEFT JOIN uf_jccs_yskm k ON s.yskm=k.id  where s.mainid = " + id;
//判断参数
            String budgetItem = (String)params.get("budgetItem");
            if(budgetItem!=null&&!budgetItem.isEmpty()){
                sqlDt1+=(" AND s.ysxm = ? ");
                sqlSons.add(budgetItem);
            }

            String budgetAccount = (String)params.get("budgetAccount");
            if(budgetAccount!=null&&!budgetAccount.isEmpty()){
                sqlDt1+=(" AND k.yskmbm = ? ");
                sqlSons.add(budgetAccount);
            }

            String exeUnit = (String)params.get("exeUnit");
            if(exeUnit!=null&&!exeUnit.isEmpty()){
                sqlDt1+=(" AND s.zxdw = ? ");
                sqlSons.add(exeUnit);
            }

            String event = (String)params.get("event");
            if(event!=null&&!event.isEmpty()){
                sqlDt1+=(" AND s.yssx = ? ");
                sqlSons.add(event);
            }

            String billNo = (String)params.get("billNo");
            if(billNo!=null&&!billNo.isEmpty()){
                sqlDt1+=(" AND s.dh = ? ");
                sqlSons.add(billNo);
            }




            recordSet1.executeQuery(sqlDt1,sqlSons);
            ArrayList<Map> yszybDt1s = new ArrayList<>();
            while(recordSet1.next()){
                HashMap<String, Object> dt1Map = new HashMap<>();
                dt1Map.put("ysnd", Util.null2String(recordSet1.getString("ysnd")));
                dt1Map.put("zdgz", Util.null2String(recordSet1.getString("dwjzdgz")));
                dt1Map.put("ysxm", Util.null2String(recordSet1.getString("ysxm")));
                dt1Map.put("yskm", Util.null2String(recordSet1.getString("yskmbm")));
                dt1Map.put("yssx", Util.null2String(recordSet1.getString("yssx")));
                dt1Map.put("zxdw", Util.null2String(recordSet1.getString("zxdw")));
                dt1Map.put("dh", Util.null2String(recordSet1.getString("dh")));
                dt1Map.put("rq", Util.null2String(recordSet1.getString("rq")));
                dt1Map.put("zyqcje", Util.null2String(recordSet1.getString("zyqcje")));
                dt1Map.put("zytzje", Util.null2String(recordSet1.getString("zydzje")));
                dt1Map.put("zyje", Util.null2String(recordSet1.getString("zyje")));
                dt1Map.put("zxje", Util.null2String(recordSet1.getString("zhje")));
                dt1Map.put("zyye", Util.null2String(recordSet1.getString("zyye")));
                dt1Map.put("zxdjlx", Util.null2String(recordSet1.getString("zhdjlx")));
                yszybDt1s.add(dt1Map);
            }
            //查询从表并封装
            map.put("detail",yszybDt1s);
            list.add(map);
        }
            //查询从表并封装
        req.put("data",list);
        return req;
    }
}
